<?php
namespace App\Services\Article\Article;

use App\Models\Article\Article;
use App\Models\Article\ArticleClassify;
use App\Services\Manage\ManageIndex;
use Illuminate\Support\Facades\DB;

/**
 * 文章微应用逻辑层
 * Class ArticleLogic
 * @package App\Services\Article\Article
 */
class ArticleLogic
{
    /**
     * 文章添加
     * @return array
     */
    public function add($data)
    {
        $ids = $data['department_ids'];

        //如果发布给全部部门
        if (strpos($ids, ',')) {
            $idArr = explode(',', $ids);
            $res = true;
            $idRes = '';
            foreach ($idArr as $v) {
                $newData['department_ids'] = $v;
                $newData['content'] = $data['content'];
                $r = Article::insertGetId($newData);
                $idRes .= $r;
                if (!$r) {
                    $res = false;
                    continue;
                }
            }
            if ($res === false) {
                return auto_failure($data, '文章添加失败！');
            }
            return auto_return(['ids' => $idRes], '添加成功！');
        };
        $rst = Article::insertGetId($data);
        if ($rst === false) {
            return auto_failure($data, '文章添加失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章未作改动！');
        }
        return auto_return(['id' => $rst], '添加成功！');
    }

    /**
     * 文章编辑
     */
    public function edit($data)
    {
        $rst = Article::where('id','=', $data['id'])
            ->update($data);
        if ($rst === false) {
            return auto_failure($data, '文章编辑失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章未做改动！');
        }

        return auto_return(['number' => $rst], '文章编辑成功！');
    }

    /**
     * 某篇文章置顶
     * @param $data
     * @return array
     */
    public function top($data)
    {
        $updateData['top'] = 1;
        $updateData['update_time'] = time();
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章置顶成功！');
    }

    /**
     * 取消某篇文章置顶
     * @param $data
     */
    public function cancelTop($data)
    {
        $updateData['top'] = 0;
        $updateData['update_time'] = time();
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章已取消置顶！');
    }

    /**
     * 文章推荐
     */
    public function recommended($data)
    {
        $updateData['recommended'] = 1;
        $updateData['update_time'] = time();
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章已推荐！');
    }

    /**
     * 文章取消推荐
     * @param $data
     */
    public function cancelRecommended($data)
    {
        $updateData['recommended'] = 0;
        $updateData['update_time'] = time();
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章已取消推荐！');
    }

    /**
     * 删除某篇文章
     * @param $data
     * @return array
     */
    public function delete($data)
    {
        $updateData['status'] = 0;
        $updateData['update_time'] = time();
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章已删除！');
    }

    /**
     * 恢复启用某篇文章
     * @param $data
     * @return array
     */
    public function enable($data)
    {
        $updateData['status'] = 1;
        $updateData['update_time'] = time();
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章已回复启用！');
    }

    /**
     * 上架重新发布某篇文章
     * @param $data
     * @return array
     */
    public function release($data)
    {
        $updateData['release_status'] = 1;
        $updateData['update_time'] = TIME_STAMP;
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章已启用！');
    }

    /**
     * 下架发布某篇文章
     * @param $data
     * @return array
     */
    public function cancelRelease($data): array
    {
        $updateData['release_status'] = 0;
        $updateData['update_time'] = TIME_STAMP;
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章已禁用！');
    }

    /**
     * 文章阅读权限 仅自己可见
     * @param $data
     * @return array
     */
    public function oneselfVisible($data): array
    {
        $updateData['arcrank'] = 0;
        $updateData['update_time'] = time();
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    /**
     * 阅读权限 所有人可见
     * @param $data
     * @return array
     */
    public function everyoneVisible($data): array
    {
        $updateData['arcrank'] = 1;
        $updateData['update_time'] = time();
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    /**
     * 阅读权限 指定人可见
     * @param $data
     * @return array
     */
    public function nominatorVisible($data): array
    {
        $updateData['arcrank'] = 2;
        $updateData['update_time'] = time();
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    /**
     * 阅读权限 指定人不可见
     * @param $data
     * @return array
     */
    public function nominatorInvisible($data): array
    {
        $updateData['arcrank'] = 3;
        $updateData['update_time'] = time();
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    /**
     * 评论权限 允许评论
     * @param $data
     * @return array
     */
    public function review($data): array
    {
        $updateData['review_status'] = 1;
        $updateData['update_time'] = time();
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    /**
     * 评论权限 禁止评论
     * @param $data
     * @return array
     */
    public function banReview($data): array
    {
        $updateData['review_status'] = 0;
        $updateData['update_time'] = time();
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    /**
     * 转载权限 允许转载
     * @param $data
     * @return array
     */
    public function reprinted($data): array
    {
        $updateData['reprinted_status'] = 1;
        $updateData['update_time'] = time();
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    /**
     * 转载权限 禁止转载
     * @param $data
     * @return array
     */
    public function banReprinted($data): array
    {
        $updateData['reprinted_status'] = 0;
        $updateData['update_time'] = time();
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    /**
     * 审核通过
     * @param $data
     * @return array
     */
    public function audit($data): array
    {
        $updateData['audit_status'] = 1;
        $updateData['audit_id'] = $data['audit_id'];
        $updateData['audit_time'] = time();
        $updateData['update_time'] = time();
        $rst = Article::where('id', $data['id'])
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    /**
     * 审核失败
     * @param $data
     * @return array
     */
    public function auditFailure($data): array
    {
        $updateData['audit_status'] = -1;
        $updateData['audit_id'] = $data['audit_id'];
        $updateData['audit_time'] = time();
        $updateData['update_time'] = time();
        $rst = Article::where('id', $data['id'])
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    /**
     * 文章可见人指定
     * @param $data
     * @return array
     */
    public function visible($data): array
    {
        $updateData['visible'] = json_encode($data["visible"]);
        $updateData['update_time'] = time();
        $rst = Article::where('id', $data['id'])
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '指定可见人数量已修改！');
    }

    /**
     * 指定文章不可见
     * @param $data
     * @return array
     */
    public function invisible($data): array
    {
        $updateData['invisible'] = json_encode($data["invisible"]);
        $updateData['update_time'] = time();
        $rst = Article::where('id', $data['id'])
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '指定不可见人数量已修改！');
    }

    /**
     * 点赞次数 +1
     * @param $data
     * @return array
     */
    public function praise($data): array
    {
        $praiseNum = Article::where($data)->value('praise');
        $updateData['praise'] = $praiseNum + 1;
        $updateData['update_time'] = time();
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章点赞次数已更新！');
    }

    /**
     * 踩 次数 +1
     * @param $data
     * @return array
     */
    public function trample($data): array
    {
        $trampleNum = Article::where($data)->value('trample');
        $updateData['trample'] = $trampleNum + 1;
        $updateData['update_time'] = time();
        $rst = Article::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure($data, '文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure($data, '文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章被踩次数已更新！');
    }

    /**
     * 文章列表
     * @param $data
     * @return array
     */
    public function articleList($data)
    {
//          $que = Article::join('department', 'department.id', '=', 'article.department_ids')
//            ->where('article.status','<>',0)
//            ->select('article.*', 'department.name_cn');
//            if(!empty($data['department_ids'])){
//               $que->where('article.department_ids','=',$data['department_ids']);
//            }
//             $list = $que->orderBy('article.update_time', 'desc')->paginate(10);
        $list = Article::with(['department'])
            ->orderBy('update_time', 'desc')
            ->paginate(10);
        return auto_success($list, '成功获取文章列表', '获取文章列表失败');
    }

    /**
     * 获取某一类置顶状态文章列表 非删除
     * @param $data
     * @return array
     */
    public function topList($data): array
    {
        $list = Article::where([['classify_id', '=', $data['classify_id']], ['status', '=', 1], ['top', '=', 1], ['recommended', '=', 0],
            ['release_status', '=', 1], ['arcrank', '=', 1], ['audit_status', '=', 1]])
            ->orderBy('update_time', 'desc')
            ->paginate($data['listRows'], null, null, $data['page'])
            ->toArray();
        return auto_return_page($list);
    }

    /**
     * 获取某一类推荐状态文章列表 非删除
     * @return mixed
     */
    public function recommendedList($data): array
    {
        $list = Article::where([['classify_id', '=', $data['classify_id']], ['status', '=', 1], ['top', '=', 0], ['recommended', '=', 1],
            ['release_status', '=', 1], ['arcrank', '=', 1], ['audit_status', '=', 1]])
            ->orderBy('update_time', 'desc')
            ->paginate($data['listRows'], null, null, $data['page'])
            ->toArray();
        return auto_return_page($list);
    }

    /**
     * 获取某一篇文章信息 非删除
     */
    public function articleInfo($data): array
    {
        $res = Article::leftJoin('article_classify', 'article_classify.id', '=', 'article.classify_id')
            ->select('article.*', 'article_classify.name')->where([['article.id', '=', $data['id']], ['article.status', '=', 1]])->first();

        if (!empty($res)) {
            $res['cate_ids'] = (new ArticleclassifyLogic())->getParentIds($res['classify_id']);
            return auto_return($res);
        }
        return auto_failure($res, "查询文章不存在！");
    }
}